package cn.iocoder.yudao.module.forward.service.datasourceinfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.forward.controller.admin.datasourceinfo.vo.DataSourceInfoPageReqVO;
import cn.iocoder.yudao.module.forward.controller.admin.datasourceinfo.vo.DataSourceInfoSaveReqVO;
import cn.iocoder.yudao.module.forward.dal.dataobject.datasource.DataSourceDO;
import cn.iocoder.yudao.module.forward.dal.dataobject.datasourceinfo.DataSourceInfoDO;
import cn.iocoder.yudao.module.forward.dal.mysql.datasource.DataSourceMapper;
import cn.iocoder.yudao.module.forward.dal.mysql.datasourceinfo.DataSourceInfoMapper;
import cn.iocoder.yudao.module.forward.enums.DfsErrorCodeConstants;
import cn.iocoder.yudao.module.forward.mqtt.MqttInfoCreateUtil;
import cn.iocoder.yudao.module.forward.mqtt.MqttManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 信息源配置 Service 实现类
 *
 * @author dfs
 */
@Slf4j
@Service
@Validated
public class DataSourceInfoServiceImpl implements DataSourceInfoService {

    @Resource
    private DataSourceInfoMapper dataSourceInfoMapper;
    @Resource
    private DataSourceMapper dataSourceMapper;
    @Resource(name = "SourceMqttManager")
    private MqttManager mqttManager;

    @Override
    public Integer createDataSourceInfo(DataSourceInfoSaveReqVO createReqVO) {
        // 插入
        DataSourceInfoDO dataSourceInfo = BeanUtils.toBean(createReqVO, DataSourceInfoDO.class);
        dataSourceInfoMapper.insert(dataSourceInfo);
        DataSourceDO sourceDO = dataSourceMapper.selectById(dataSourceInfo.getSourceId());
        MqttInfoCreateUtil.tryDataSourceConnect(mqttManager, sourceDO, dataSourceInfo);
        // 返回
        return dataSourceInfo.getId();
    }

    @Override
    public void updateDataSourceInfo(DataSourceInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateDataSourceInfoExists(updateReqVO.getId());
        // 更新
        DataSourceInfoDO updateObj = BeanUtils.toBean(updateReqVO, DataSourceInfoDO.class);
        dataSourceInfoMapper.updateById(updateObj);
        DataSourceDO sourceDO = dataSourceMapper.selectById(updateObj.getSourceId());
        MqttInfoCreateUtil.tryDataSourceConnect(mqttManager, sourceDO, updateObj);
    }

    @Override
    public void deleteDataSourceInfo(Integer id) {
        // 校验存在
        validateDataSourceInfoExists(id);
        // 删除
        dataSourceInfoMapper.deleteById(id);
    }

    private void validateDataSourceInfoExists(Integer id) {
        if (dataSourceInfoMapper.selectById(id) == null) {
            throw exception(DfsErrorCodeConstants.DATA_SOURCE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public DataSourceInfoDO getDataSourceInfo(Integer id) {
        return dataSourceInfoMapper.selectById(id);
    }

    @Override
    public PageResult<DataSourceInfoDO> getDataSourceInfoPage(DataSourceInfoPageReqVO pageReqVO) {
        return dataSourceInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public DataSourceInfoDO getDataSourceInfoBySourceId(Integer sourceId) {
        return dataSourceInfoMapper.selectOne(DataSourceInfoDO::getSourceId, sourceId);
    }
}